<?php
	//开户PHP接口
	//create by L
	//http://42.121.57.193:7788/platform/register.php?flag=000000&phone=15507558558

	//初始化--
	include_once "init.php";

	$client_flag=$_GET["flag"];
	$client_phonenumber=$_GET["phone"];

	if('' == $client_flag )
	{
		die("-1|用户编码不可以为空");
	}
	if('' == $client_phonenumber)
	{
		die("-1|手机号码不可以为空");
	}
	if(false==ctype_alnum($client_flag))
	{
		die("-1|无效用户编码");
	}

	if (strlen($client_phonenumber) != 11 || false==is_numeric($client_phonenumber) || ord($client_phonenumber)!=49)
	{
		die("-1|手机号码必须是11位数字，并且以1开头");
	}

	if (strlen($client_flag) != 6)
	{
		die('-1|flag参数错误');
	}
	//取一个费率最低的线路ID
	$webline_id = $webline_config["0"]["id"];
	$sipline_id = $sipline_config["0"]["id"];


	//SIP账户和密码
	$sip_account = $client_phonenumber.srand_ext(0,10000);
	$sip_pwd 	 = srand_ext(100000,999999);

	$client_password=$client_phonenumber;

	$con = db_connect(DB_NAME);

	//检测用户是否存在
	$res = mysql_query("SELECT phone from user where phone='$client_phonenumber'") or die("-1|查询数据库失败");
	$row = mysql_fetch_row($res);

	if (!empty($row))
	{
		mysql_close($con);
		die("1|该手机号码已经注册");
	}

	//以下数据库操作获取UUID
	mysql_query("LOCK TABLES user WRITE");
	mysql_query("INSERT INTO user (flag,phone,band_phone,password,webline_id,sipline_id,sip_account,sip_pwd,grade,points) VALUES ('".$client_flag."','".$client_phonenumber."','".$client_phonenumber."','".$client_password."',".$webline_id.",".$sipline_id.",'".$sip_account."','".$sip_pwd."','0','1000')");
	$result=mysql_query("SELECT LAST_INSERT_ID() AS new_uuid");
	mysql_query("UNLOCK TABLES");

	if(FALSE==$result)
	{
		mysql_close($con);
		die("-1|查询数据库失败");
	}
	$row = mysql_fetch_array($result);

	if(FALSE==$row)
	{
		mysql_close($con);
		die("-1|获取数据集失败");
	}
	mysql_close($con);

	$client_uuid=$row['new_uuid'];

	if(''==$client_uuid||0==$client_uuid)
	{
		die("-1|空数据集错误");
	}

	//1 step--用新UUID在VOS上进行注册
	$type_1		  		= '0';
	$account_1      	= 'a_'.$client_uuid;
	$accountType_1  	= '1';				//0：普通<默认>；1：电话卡；2：结算
	$name_1 		  	= 'n_'.$client_uuid;
	$money_1 		  	= 0.00;
	$limitMoney_1	  	= 0.00;
	$feerateGroup_1 	= $webline_config["0"]["feerate"];			//费率使用最低的
	$lockType_1     	= 0;
	$validTime_1	  	= strtotime("+10 year").'000';
	$loginName_1	   	= VOS_USER;
	$loginPassword_1 	= VOS_PW;

	//调用VOS接口--注册用户
	$result = setcustomer($type_1,$account_1,$accountType_1,$name_1,$money_1,$limitMoney_1,$feerateGroup_1,$lockType_1,$validTime_1,$loginName_1,$loginPassword_1);

	if('0'!=$result[0]&&'|'!=$result[1])
	{
		$con = db_connect(DB_NAME);
		mysql_query("DELETE FROM user WHERE uuid=".$client_uuid);
		mysql_close($con);
		setcustomer('2',$account_1,$accountType_1,$name_1,$money_1,$limitMoney_1,$feerateGroup_1,$lockType_1,$validTime_1,$loginName_1,$loginPassword_1);
		die("-1|创建电话卡账户失败");
	}

	//2 step --给新用户开一个新电话卡--
	$type_2 			= '0';
	$pin_2  			= 'c_'.$client_uuid;
	$password_2 		= 'p_'.$client_uuid;
	$account_2	  		= 'a_'.$client_uuid;
	$loginName_2	   	= VOS_USER;
	$loginPassword_2 	= VOS_PW;

	//VOS接口--开新卡
	$result = setactivephonecard($type_2,$pin_2,$password_2,$account_2,$loginName_2,$loginPassword_2);

	if('0'!=$result[0]&&'|'!=$result[1])
	{
		$con = db_connect(DB_NAME);
		mysql_query("DELETE FROM user WHERE uuid=".$client_uuid);
		setactivephonecard('2',$pin_2,$password_2,$account_2,$loginName_2,$loginPassword_2);
		setcustomer('2',$account_1,$accountType_1,$name_1,$money_1,$limitMoney_1,$feerateGroup_1,$lockType_1,$validTime_1,$loginName_1,$loginPassword_1);
		mysql_close($con);
		die("-1|创建电话卡失败");
	}

	//3 step --电话卡绑定号码
	$type_3 				= '0';
	$e164_3 				= $client_phonenumber;
	$activePhoneCard_3 		= 'c_'.$client_uuid;
	$loginName_3	   		= VOS_USER;
	$loginPassword_3 		= VOS_PW;

	//VOS接口--电话卡绑定号码
	$result = setbindede164($type_3,$e164_3,$activePhoneCard_3,$loginName_3,$loginPassword_3);
	if('0'!=$result[0]&&'|'!=$result[1])
	{
		$con = db_connect(DB_NAME);
		mysql_query("DELETE FROM user WHERE uuid=".$client_uuid);
		setbindede164('2',$e164_3,$activePhoneCard_3,$loginName_3,$loginPassword_3);
		setactivephonecard('2',$pin_2,$password_2,$account_2,$loginName_2,$loginPassword_2);
		setcustomer('2',$account_1,$accountType_1,$name_1,$money_1,$limitMoney_1,$feerateGroup_1,$lockType_1,$validTime_1,$loginName_1,$loginPassword_1);
		mysql_close($con);

		die("-1|绑定号码失败");
	}

	//4 setp --直拨话机操作
	$type_4 			= '0';
	$e164_4   			= $sip_account;
	$password_4 		= $sip_pwd;
	$displayNumber_4 	= $client_phonenumber;
	$lockType_4 		= '0';
	$callLevel_4 		= '4';
	$rewriteRule_4 		= '';
	$calleeBilling_4 	= '0';
	$capacity_4			= '2';
	$webPassword_4		= $sip_pwd;
	$account_4			= 'a_'.$client_uuid;
	$loginName_4	   	= VOS_USER;
	$loginPassword_4 	= VOS_PW;

	$result = setphone(
					 $type_4,				//0：创建；1：修改；2：删除 		--必填
					 $e164_4,				//话机号码（全为数字） 				--必填
					 $password_4,			//话机密码
					 $displayNumber_4,		//去电显示
					 $lockType_4,			//0:无锁定1:锁定呼出2:锁定呼入3:全部锁定
					 $callLevel_4,			//1:网内通话2:本地市话4:国内长途5:国际长途
					 $rewriteRule_4,		//被叫改写规则--切换线路
					 $calleeBilling_4,		//被叫付费 0:关闭1:启用
					 $capacity_4,			//线路数量
					 $webPassword_4,		//话机Web查询密码
					 $account_4,			//账户号码
					 $loginName_4,			//操作用户 			--必填
					 $loginPassword_4		//操作用户密码		--必填
						);

	if('0'!=$result[0]&&'|'!=$result[1])
	{
		$con = db_connect(DB_NAME);
		mysql_query("DELETE FROM user WHERE uuid=".$client_uuid);
		setphone(
					 '2',
					 $e164_4,
					 $password_4,
					 $displayNumber_4,
					 $lockType_4,
					 $callLevel_4,
					 $rewriteRule_4,
					 $calleeBilling_4,
					 $capacity_4,
					 $webPassword_4,
					 $account_4,
					 $loginName_4,
					 $loginPassword_4
						);
		setbindede164('2',$e164_3,$activePhoneCard_3,$loginName_3,$loginPassword_3);
		setactivephonecard('2',$pin_2,$password_2,$account_2,$loginName_2,$loginPassword_2);
		setcustomer('2',$account_1,$accountType_1,$name_1,$money_1,$limitMoney_1,$feerateGroup_1,$lockType_1,$validTime_1,$loginName_1,$loginPassword_1);

		mysql_close($con);

		die("-1|创建话机失败");
	}
	else
	{
		$con = db_connect(DB_NAME);

		//--查询邀请表，邀请号码和注册号码一致，则进行积分赠送--
		$result = mysql_query("SELECT * FROM invite WHERE invite_phone='$client_phonenumber' ORDER BY invite_time ASC") or die("-1|查询invite表失败");
		$row = mysql_fetch_row($result);
		$phone = $row['0'];
		$invite_phone = $row['1'];
		$flag = $row['4'];
		$type = $row['2'];

		if (!empty($row))
		{

			//判断是否已经赠送积分--
			if ($flag == '0')
			{

				if($type == '1')//注册邀请--100积分
				{
					$invite_points = 100;
				}
				else if ($type == '2')//充值邀请--1000积分
				{
					$invite_points = 1000;
				}
				//获取发起邀请者的号码，修改该账户号码积分-
				$result = mysql_query("SELECT points from user where phone='$phone'") or die("-1|查询user表失败");
				$points_row = mysql_fetch_row($result);

				if (!empty($points_row))
				{
					$db_points = $points_row['0'];
					$current_points  = $db_points + $invite_points;

					//更新积分
					$result = mysql_query("UPDATE user SET points='$current_points' where phone='$phone'") or die("-1|更新user失败");

					if ( $result === false )
					{
						die("-1|增加邀请积分失败");
					}
					//更新邀请表中记录的flag值-置为1
					mysql_query("UPDATE invite SET flag='1' where invite_phone=$invite_phone") ;
				}

			}
		}
		mysql_close($con);
		echo "0|";
	}
?>
